/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.huikecloud.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.tool.api.R;
import org.springblade.modules.huikecloud.entity.JmeterNode;
import org.springblade.modules.huikecloud.utils.IPUtil;
import org.springblade.modules.huikecloud.vo.JmeterNodeVO;
import org.springblade.modules.huikecloud.mapper.JmeterNodeMapper;
import org.springblade.modules.huikecloud.service.IJmeterNodeService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.resource.entity.Oss;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * 主机表 服务实现类
 *
 * @author Blade
 * @since 2021-08-23
 */
@Service
public class JmeterNodeServiceImpl extends BaseServiceImpl<JmeterNodeMapper, JmeterNode> implements IJmeterNodeService {

    @Override
    public IPage<JmeterNodeVO> selectNodePage(IPage<JmeterNodeVO> page, JmeterNodeVO node) {
        return page.setRecords(baseMapper.selectNodePage(page, node));
    }

    @Override
    public R debug(JmeterNode jmeterNode) {
        boolean portUsing = IPUtil.isPortUsing(jmeterNode.getNodeIp(), jmeterNode.getNodePort());
        if (portUsing) {
            return R.success("压力机运行正常");
        }
        return R.fail("压力机状态异常");
    }

    @Override
    public R enable(JmeterNode jmeterNode) {
        if (jmeterNode.getStatus() == 0) {//启用
            boolean portUsing = IPUtil.isPortUsing(jmeterNode.getNodeIp(), jmeterNode.getNodePort());
            if (portUsing) {
                jmeterNode.setStatus(1);
            } else {
                return R.fail("压力机状态异常");
            }
        } else {//禁用
            jmeterNode.setStatus(0);
        }
		boolean status = this.saveOrUpdate(jmeterNode);
		//更新容器
		this.updateAllNode();
		return R.status(status);
    }

    @Override
    public R submit(JmeterNode jmeterNode) {
        //禁用
        if (CommonConstant.allNode != null) {
            CommonConstant.allNode.remove(jmeterNode.getNodeIp() + ":" + jmeterNode.getNodePort());
        }
        jmeterNode.setStatus(0);

        return R.status(this.saveOrUpdate(jmeterNode));
    }

    //更新容器
	public void updateAllNode() {
		if (CommonConstant.allNode == null) {
			CommonConstant.allNode = new HashSet<>();
		}
		ArrayList<String> nodes = new ArrayList<>();
		List<JmeterNode> list = this.list(Wrappers.<JmeterNode>query().lambda().eq(JmeterNode::getStatus, 1));
		for (JmeterNode node : list) {
			nodes.add(node.getNodeIp() + ":" + node.getNodePort());
		}
		CommonConstant.allNode.clear();
		CommonConstant.allNode.addAll(nodes);
	}
}
